package cn.usian.controller;

import cn.usian.common.R;
import cn.usian.dto.DishDto;

import cn.usian.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@RequestMapping("dish")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    @GetMapping("page")
    public R findPage(Integer page,Integer pageSize,@RequestParam(required = false) String name){
        return dishService.findPage(page,pageSize,name);
    }

    @PostMapping()
    public R add(@RequestBody DishDto dishDto, HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        dishDto.setCreateUser(id);
        dishDto.setUpdateUser(id);
        return dishService.add(dishDto);
    }

    @GetMapping("{id}")
    public R findById(@PathVariable Long id){
        return dishService.findById(id);
    }

    @PutMapping()
    public R update(@RequestBody DishDto dishDto,HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        dishDto.setCreateUser(id);
        dishService.update(dishDto);
        return R.success("修改成功");
    }


}
